<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Form_lib
{
    private $label = array();
    private $comp = array();
    private $form;
    private $CI;
    private $compSubmit = array();
        
    public function __construct()
    {
        //parent::__construct();
        //Cargamos el Helper para el uso del BASE_URL()
        //$this->load->helper('url');
        $this->CI =& get_instance();
        $this->CI->load->helper('form');
    }
     
    public function label($label, $name, $class){
         $largo = count($this->label);
         $attributes = array(
            'class'       => $class,
         );
         $this->label[$largo]=form_label($label, $name,$attributes);

    }

    public function input($name='', $value='', $class=''){
           $dataInput = array(
                      'name'        => $name,
                      'id'          => $name,
                      'value'       => $value,
                      'class'       => $class,
           );
         $largo = count($this->comp);
         $this->comp[$largo]=form_input($dataInput);
    }

    public function hidden($name='', $value=''){
           $dataHidden = array(
                      $name        => $value,
           );
         $largo = count($this->comp);
         $this->comp[$largo]=form_hidden($dataHidden);;
    }

    function comboBox($name='', $model, $func, $class='', $valorSelect='', $size='1'){
             $this->CI->load->model($model);
             $data=$this->CI->$model->$func();

             $option = array();
             foreach ( $data as $key => $value )
             {
                 $option[$key] = $value;
             }



             $largo = count($this->comp);
             $combo="<select name=\"$name\" size=\"$size\" id=\"$name\" class=\"$class\" validate=\"required:true\">";
             $combo=$combo."<option value=\"\">SELECCIONE</option>";
             //SACANDO VALORES DE TABLA PARA COMBOBOX
             $query = "SELECT * FROM $table ORDER BY $order";
             $result = mysql_query($query);
             while ($row = mysql_fetch_array($result, MYSQL_BOTH)){
                 if($row[0] == $valorSelect){
                  $selected = 'selected';
                 }else{$selected = '';}
                 $combo=$combo."<option value='$row[0]' $selected>".utf8_encode($row[1])."</option>";
             }
             //SACANDO VALORES DE TABLA PARA COMBOBOX
                 $combo=$combo."</select>";
                 $this->comp[$largo]=$combo;
    }



    public function submit($name='', $value=''){
         $largo = count($this->compSubmit);
         $this->compSubmit[$largo]=form_submit($name, $value);;
    }


    public function create($filas, $columnas, $ancho='100%', $arrayAncho, $classEnc='', $classImp='', $classPar='',$action){
         $form=form_open($action);
         $form=$form."<table width=\"$ancho\" border=\"0\">";

         $flag=0;
         $LabelCount=0;
         $CompCount=0;
         for($i=1;$i<=$filas;$i++){
             $form=$form."<tr>";
             for($j=0;$j<$columnas;$j++){
                 $form=$form."   <td width=\"$arrayAncho[$j]\" align=\"left\">";
                 if($flag == 0){
                          //echo$flag;
                          //echo$LabelCount;
                          $form=$form.$this->label[$LabelCount];
                          $flag=1;
                          $LabelCount=$LabelCount+1;
                 }else{
                          //echo$flag;
                          //echo$CompCount;
                          $form=$form.$this->comp[$CompCount];
                          $flag=0;
                          $CompCount=$CompCount+1;
                  }
                  $form=$form."   </td>";
            }
            $form=$form."</tr>";
         }
         $form=$form."<tr>";
         $form=$form."   <td colspan=\"$columnas\">";
         for($i=0;count($this->compSubmit)>$i;$i++){
             $form=$form.$this->compSubmit[$i];
         }
         $form=$form."   </td>";
         $form=$form."</tr>";
         $form=$form."</table>";
         $form=$form.form_close();
         return $form;
 }

}